<?php
namespace app\admin\controller;

use app\constdir\ApiConst;
use app\model\AdminUser;
use think\Controller;
use app\admin\service\Menu;

class Base extends Controller
{
    protected $mAdminUser;

    public function _initialize()
    {
        parent::_initialize();
        $this->mAdminUser = new AdminUser();
        $userName = $this->checkLogin();
        $user_id = input("session.user_id");
        $this->assign("user_id", $user_id);
        $this->assign('userName',$userName);
        $sMenu = new Menu();
        $menulist = $sMenu->getMenuList();
        $this->assign('menulist',$menulist);
    }

    /**
     * @auther zlq
     * @create_time 2020/5/21 11:10
     * @description 判断用户登录
     */
    public function checkLogin()
    {
        $user_id = session('user_id');
        if (!$user_id) {
            $this->redirect(url('admin/Index/login'));
        } else {
            $user = $this->mAdminUser->where(['id' => $user_id])->find();
            if (empty($user)) {
                $this->redirect(url('admin/Index/login'));
            } else {
                session('user_id', $user_id);
                session('user_name', $user['user_name']);
                session('login_name', $user['login_name']);//用户登录名
                session('is_admin', $user['is_admin']);//是否超级管理员
                session('menu_auth', $user['menu_auth']);//用户权限id
                $this->mAdminUser->where(['id' => $user_id])->update(['last_date' => date('Y-m-d'), 'last_time' => date('H:i:s')]);
            }
            return $user['user_name'];
        }
    }

    /*
    * 成功返回提示
    */
    public function apiSuccess($message, $data = null, $page = null){
        return collection([
            'status' => JSON_SUCCESS_STATUS,
            'msg' => $message,
            'data' => $data,
            'page' => $page
        ]);
    }

    /*
    * 错误提示
    */
    public function apiError($message, $code = JSON_FAIL_STATUS, $data = null){
        return collection([
                'status' => $code,
                'msg' => $message,
                'data' => $data,
            ]
        );
    }
}